package domain;

import java.util.Vector;

import basic.CRUDable;

//@author A0098891H
public class Category extends CRUDable {
	//attributes
	private String _name;
	private Vector<Category> _subCategories;
	
	/**
	 * 
	 */
	public Category()
	{
		_subCategories = null;
		_name = "";
	}
	
	/**
	 * @param cat
	 */
	public Category(Category cat)
	{
		this.set(cat);
	}
	
	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	public void initSubCategory()
	{
		_subCategories = new Vector<Category>();
	}
	
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean hasSubCategory()
	{
		if(_subCategories==null)
			return false;
		else 
			return true;
	}

	/**
	 * 
	 * @param category
	 * @Precondition 
	 * @Postcondition 
	 */
	public void set(Category category)
	{
		this._name = category._name;
		this._subCategories = category._subCategories;
	}

	/**
	 * 
	 * @param category
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean addSubCategory(Category category)
	{
		return this._subCategories.add(category);
	}
	
	/**
	 * 
	 * @param category
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public boolean isSame(Category category)
	{
		//TODO - sub-category
		if(this._name.equalsIgnoreCase(category._name))
				return true;
		return false;
	}
	
	/**
	 *
	 * @see java.lang.Object#toString()
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public String toString()
	{
		return this._name.toUpperCase();
	}
	
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public String writeToString()
	{
		String str = this._name + "," + this._id;
		if(this._subCategories!=null)
		{
			str += "\n:\t";
			for(Category cate: _subCategories)
			{
				str+=" "+cate._name;
			}
		}
		return str;
	}
	
	//getters and setters
	/**
	 * 
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	public String get_name() {
		return _name;
	}

	/**
	 * 
	 * @param name
	 * @Precondition 
	 * @Postcondition 
	 */
	public void set_name(String name) {
		this._name = name.toUpperCase();
	}
}
